package com.greateffect.littlebud.lib.audio;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Handler;

import com.greateffect.littlebud.lib.utilcode.Utils;
import com.greateffect.littlebud.lib.utils.JLogUtil;

/**
 * Created by ZengCS on 2019/9/11.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 */
public class AudioPlayer {
    private static final String TAG = "LearningActivity_";
    private PlayCompleteListener playCompleteListener;

    public void setPlayCompleteListener(PlayCompleteListener playCompleteListener) {
        this.playCompleteListener = playCompleteListener;
    }

    public interface PlayCompleteListener {
        void doNext();
    }

    public void playAudio(Context context, int audioId) {
        playAudio(context, audioId, null);
    }

    private MediaPlayer mCurrMediaPlayer;

    public void playAudio(Context context, int audioId, PlayCompleteListener playCompleteListener) {
        stop();

        try {
            mCurrMediaPlayer = MediaPlayer.create(context, audioId);
            mCurrMediaPlayer.start();
            JLogUtil.i(TAG, "开始播放@" + audioId);
            new Handler().postDelayed(() -> {
                JLogUtil.i(TAG, "播放结束@" + audioId);
                if (playCompleteListener != null)
                    playCompleteListener.doNext();
            }, mCurrMediaPlayer.getDuration());
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    public void play(String url) {
        this.play(url, null);
    }

    public void play(String url, PlayCompleteListener playCompleteListener) {
        JLogUtil.d(TAG, "play() called with: url = [" + url + "]");
        stop();
        this.playCompleteListener = playCompleteListener;
        mCurrMediaPlayer = new MediaPlayer();
        try {
            if (url.startsWith("file:")) {
                AssetManager assetManager = Utils.getApp().getAssets();
                AssetFileDescriptor fileDescriptor = assetManager.openFd(url.replace("file:///android_asset/", ""));
                mCurrMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                        fileDescriptor.getStartOffset());
            } else {
                mCurrMediaPlayer.setDataSource(url);
            }
            mCurrMediaPlayer.prepare();
//            mCurrMediaPlayer.setOnCompletionListener(mp -> {
//                if (playCompleteListener != null)
//                    playCompleteListener.doNext();
//                // release(mp);
//            });
            JLogUtil.i(TAG, "开始播放@" + url);
            mCurrMediaPlayer.start();
            JLogUtil.d(TAG, "getDuration() = " + mCurrMediaPlayer.getDuration() + "ms");
            new Handler().postDelayed(() -> {
                JLogUtil.i(TAG, "播放结束@" + url);
                if (playCompleteListener != null)
                    playCompleteListener.doNext();
            }, mCurrMediaPlayer.getDuration());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放播放器资源
     */
    private void release(MediaPlayer mediaPlayer) {
        if (mediaPlayer != null) {
            mediaPlayer.stop();

            //关键语句
            mediaPlayer.reset();

            mediaPlayer.release();
        }
    }

    public void stop() {
        try {
            if (mCurrMediaPlayer != null) {
                release(mCurrMediaPlayer);
                mCurrMediaPlayer = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
